package com.powernode.ssm.springAnalysis.a04;


import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor;
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver;
import org.springframework.context.support.GenericApplicationContext;

/**
 * 测试bean的后处理器，解析各个注解
 */
public class TestBeanPostProfessor4 {

	public static void main(String[] args) {
		//GenericApplicationContext不会自动创建各种bean后处理器，即不会解析@Autowired等注解
		GenericApplicationContext context = new GenericApplicationContext();
		context.registerBean("bean9", Bean9.class);
		context.registerBean("bean8", Bean8.class);
		context.registerBean("bean7", Bean7.class);
		context.registerBean("bean10", Bean10.class);

//		添加这个可以识别@Value注解，否则会报错
		context.getDefaultListableBeanFactory().setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
		context.registerBean(AutowiredAnnotationBeanPostProcessor.class); // 解析@Autowired @Value

		context.registerBean(CommonAnnotationBeanPostProcessor.class);//解析 @Resource @PostConstruct @PreDestroy

		ConfigurationPropertiesBindingPostProcessor.register(context.getDefaultListableBeanFactory());//解析 @ConfigurationProperties

		context.refresh();// 执行beanFactory后处理器, 添加bean后处理器, 初始化所有单例

		System.out.println(context.getBean(Bean10.class));


		context.close();
		/*
            学到了什么
                a. @Autowired 等注解的解析属于 bean 生命周期阶段(依赖注入, 初始化)的扩展功能
                b. 这些扩展功能由 bean 后处理器来完成
         */

	}

}
